package com.sky.service.impl;


import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class AddressBookServiceImpl implements AddressBookService {

    @Autowired(required = true)
    AddressBookMapper addressBookMapper;

    @Override
    public void add(AddressBook addressBook) {
        //重复地址不用管,用户会自己删掉的
        addressBook.setUserId(BaseContext.getCurrentId());
        //首先判断用户是否有已存在的默认地址,如果没有,就设置为默认地址
        AddressBook list = defaultList();//调用下方查询默认地址方法,应该莫得问题
        if (list != null) {
            addressBook.setIsDefault(StatusConstant.DISABLE);
        } else {
            addressBook.setIsDefault(StatusConstant.ENABLE);
        }
        log.info("服务层直接添加{}", addressBook);

        addressBookMapper.add(addressBook);
    }

    @Override
    public List<AddressBook> list() {
        log.info("服务层获取用户所有地址信息");
        AddressBook addressBook = new AddressBook();
        addressBook.setUserId(BaseContext.getCurrentId());
        return addressBookMapper.list(addressBook);
    }

    @Override
    public AddressBook defaultList() {
        log.info("服务层查询用户的默认地址");
        AddressBook addressBook = new AddressBook();
        addressBook.setUserId(BaseContext.getCurrentId());
        return addressBookMapper.defaultList(addressBook);
    }

    @Override
    public void updateById(AddressBook addressBook) {
        log.info("服务层依据id更新地址数据{}", addressBook);
        //保证修改的是当前用户的数据
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookMapper.updateById(addressBook);

    }

    @Override
    public AddressBook getById(Long id) {
        log.info("服务层依据id查询地址数据:{}", id);
        return addressBookMapper.getById(id);
    }

    @Override
    @Transactional
    public void updateDefaultById(AddressBook addressBook) {
        log.info("服务层设置当前用户的默认地址");
        //首先应当确定用户列表内的默认地址,并将其修改为非默认地址
        new AddressBook();
        AddressBook addressBooktest = AddressBook
                .builder()
                .userId(BaseContext.getCurrentId())
                .build();
        AddressBook addressBook1 = addressBookMapper.defaultList(addressBooktest);
        if (addressBook1 != null) {
            //那说明当前地址就是默认地址,不做处理
            return;
        }
        addressBook1.setIsDefault(StatusConstant.DISABLE);
        addressBookMapper.updateById(addressBook1);//更新为非默认地址
        //然后新建对象,导入id和userid,修改默认地址
        new AddressBook();
        AddressBook addressBook2 = AddressBook
                .builder()
                .id(addressBook.getId())
                .userId(BaseContext.getCurrentId())
                .isDefault(StatusConstant.ENABLE)
                .build();
        addressBookMapper.updateById(addressBook2);
    }
}
